package com.tarena.lbs.basic.web.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.basic.web.repository.AreaRepository;
import com.tarena.lbs.pojo.basic.po.AreaPO;
import com.tarena.lbs.pojo.basic.vo.AreaVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AreaService {
    @Autowired
    private AreaRepository areaRepository;
    public List<AreaVO> getAreaList(Integer parentId) {
        //select * from lbs_area where parent_id=?
        List<AreaPO> pos=getChildren(parentId);
        List<AreaVO> vos=null;
        //查询列表非空的时候 将po转化成vo
        if(pos!=null&&pos.size()>0){
            vos=pos.stream().map(po->{
                AreaVO vo=new AreaVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return vos;
    }

    private List<AreaPO> getChildren(Integer parentId) {
        QueryWrapper<AreaPO> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        return areaRepository.list(queryWrapper);
    }

    public List<AreaVO> tree() {
        //查询所有area 封装vo返回 3000多个
        List<AreaPO> pos = areaRepository.list();
        log.info("获取行政区域数据总量:{}",pos.size());
        //手动封装一下三级分类树的结构
        return assembleAreaTree(pos);
    }

    private List<AreaVO> assembleAreaTree(List<AreaPO> pos) {
        //1.先准备一个List<AreaVO> 填充省级单位 pos有省级区域 parentId==0
        List<AreaPO> provincePos=
                pos.stream().filter(po->po.getParentId()!=null&&po.getParentId()==0l).collect(Collectors.toList());
        log.info("城市区域树查询省:{}",provincePos);
        List<AreaVO> provinceVos=pos2vos(provincePos);
        //2.循环遍历这个省的列表
        for (AreaVO provinceVo : provinceVos) {
            long provinceId = provinceVo.getId();
            //3.删选遍历全量数据pos 拿到这个省下的所有市单位 Long和Integer都有常量池 -128~127 封装类数字在这个范围 ==相等的 超出这个范围 ==比较2个对象
            List<AreaPO> cityPos=pos.stream().filter(po->po.getParentId()!=null&&po.getParentId()==provinceId).collect(Collectors.toList());
            //4.将cityPos转化成cityVo给provinceVo children封装
            List<AreaVO> cityVos=pos2vos(cityPos);
            provinceVo.setChildren(cityVos);
            //5.循环遍历这个市拿到每一个城市数据
            for (AreaVO cityVo : cityVos) {
                long cityId = cityVo.getId();
                //6,筛选这个城市下的所有区
                List<AreaPO> areaPos=
                        pos.stream().filter(po->po.getParentId()!=null&&po.getParentId()==cityId).collect(Collectors.toList());
                List<AreaVO> areaVOS = pos2vos(areaPos);
                cityVo.setChildren(areaVOS);
            }
        }
        return provinceVos;
    }

    private List<AreaVO> pos2vos(List<AreaPO> pos) {
        return pos.stream().map(po->{
            AreaVO vo=new AreaVO();
            BeanUtils.copyProperties(po,vo);
            return vo;
        }).collect(Collectors.toList());
    }
}
